package com.wzy.service.imp;

import cn.hutool.core.date.DateUtil;
import com.wzy.dao.LoginLogDao;
import com.wzy.dao.UserDao;
import com.wzy.entity.LoginLog;
import com.wzy.entity.User;
import com.wzy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Date;

@Service

public class UserServiceImpl implements UserService {

    private UserDao userDao;
    @Resource
    private LoginLogDao loginLogDao;

    @Autowired
    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }


    @Override
    public User login(String username, String password) {
        //1.根据用户名查询用户
        User user = userDao.findByUserName(username);
        if (ObjectUtils.isEmpty(user)) {
            throw new RuntimeException("用户名不正确!");
        }
        //2.比较密码
        String passwordSecret = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        if (!user.getPassword().equals(passwordSecret)) {
            throw new RuntimeException("密码输入错误!");
        }
        loginLogDao.insertLog(instanceLog(user.getUsername(), "成功", "用户登录"));
        return user;
    }

    @Override
    public void register(User user) {
        //1.根据用户名查询数据库中是否存在用户
        User userDB = userDao.findByUserName(user.getUsername());
        //2.判断用户是否存在
        if (!ObjectUtils.isEmpty(userDB)) throw new RuntimeException("当前用户名已被注册!");
        //3.注册用户&明文的密码加密  特点: 相同字符串多次使用md5就行加密 加密结果始终是一致
        String newPassword = DigestUtils.md5DigestAsHex(user.getPassword().getBytes(StandardCharsets.UTF_8));
        user.setPassword(newPassword);
        userDao.save(user);
    }


    public static LoginLog instanceLog(String name, String status, String message) {
        LoginLog loginLog = new LoginLog();
        loginLog.setUserName(name);
        loginLog.setStatus(status);
        loginLog.setMsg(message);
        loginLog.setLoginTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        return loginLog;
    }

}
